<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            width: 600px;
            margin: 100px auto;
        }
        
        .message {
            display: inline-block;
            font-size: 12px;
            color: #999;
            background: url(blue.png) no-repeat left center;
            padding-left: 20px;
        }
        
        .wrong {
            color: red;
            background-image: url(red.png);
        }
        
        .right {
            color: green;
            background-image: url(green.png);
        }
    </style>
</head>

<body>
    <div class="register">
        <input type="password" class="ipt">
        <p class="message">密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线</p>
    </div>
    <script>
       var ipt = document.querySelector('.ipt');
        var message = document.querySelector('.message');
        ipt.onblur = function () {
            var reg=/^[a-zA-Z]\w{5,17}$/;
            console.dir(ipt);
            var password=ipt.value;
            var flag=password.match(reg);
            if (flag) {
                message.className = 'message right';
                message.innerHTML = '密码格式正确';
            } else {
                message.className = 'message wrong';
                message.innerHTML = '密码请以字母开头，长度在6~18之间，只能包含字母、数字和下划线';
            }
            
        }

    </script>
    <p align="center"> &copy; 版权由刘红利归属，未经允许不得复制、镜像</p>
</body>

</html>